import KvMotion from "./kv_motion";
import EventCenter from "./event_center";
import TouchEvent from "./touch_event";
import Event from "./event";
import AudioCtrl from "./audio_ctrl";

export default class ButtonKv extends KvMotion {
  soundEffect = null;
  initialize() {
    let e = this;
    e.initWithURL("views/bottom.xml"),
      (e.soundEffect = new AudioCtrl("sounds/bottom.mp3")),
      e.soundEffect.loop(!1);
  }
  stars = null;
  voc = null;
  onViewControllerPrepared() {
    let e = this;
    (e.stars = e.view().subViewByName("stars")),
      (e.voc = e.view().subViewByName("voc")),
      EventCenter.addListener(e.view(), e, TouchEvent.CLICK, function () {
        e.isPlaying == 0 && e.play();
      }),
      EventCenter.addListener(e.voc, e, Event.END, e.onVocREnd);
  }
  onViewAddToStage() {
    let e = this;
    e.prepared &&
      ((e.isPlaying = !1), e.voc.goToAndPlay(0), e.stars.goToAndStop(0));
  }
  onViewRemoveFromStage() {
    let e = this;
    e.prepared && (e.voc.stop(), e.voc.stop(), e.soundEffect.pause());
  }
  onViewUpdated() {
    let e = this;
    e.prepared &&
      e.isPlaying == 0 &&
      e.voc.currentFrame() == 15 &&
      e.voc.goToAndPlay(0);
  }
  isPlaying = !1;
  play() {
    let e = this;
    e.isPlaying == 0 &&
      ((e.isPlaying = !0), e.stars.goToAndPlay(0), e.soundEffect.play(0));
  }
  onVocREnd() {
    let e = this;
    (e.isPlaying = !1), e.voc.goToAndPlay(0);
  }
}
